package com.xiao.controller;

import com.xiao.domain.ResponseResult;
import com.xiao.domain.dto.AddMenuDto;
import com.xiao.domain.dto.MenuListDto;
import com.xiao.domain.vo.MenuListVO;
import com.xiao.domain.vo.MenuTreeSelectVo;
import com.xiao.domain.vo.RoleMenuTreeByIdVo;
import com.xiao.domain.vo.SysRoleByIdVo;
import com.xiao.service.SysMenuService;
import com.xiao.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "菜单")
@RequestMapping("/system/menu")
public class SysMenuController {
    @Autowired
    SysMenuService sysMenuService;

    @GetMapping("/list")
    @ApiOperation("获取菜单列表")
    @ApiImplicitParam(value = "menuListDto",name = "菜单列表 DTO")
    public ResponseResult<MenuListVO> menuList(MenuListDto menuListDto){

        ResponseResult<MenuListVO> result = sysMenuService.menuAllList(menuListDto);

        return result;
    }

    @PostMapping/*("/")*/ // 当前控制层 /system/menu 所以这里不需要加请求路径
    @ApiOperation("新增菜单")
    @ApiImplicitParam(name = "addMenuDto",value = "新增菜单-DTO")
    public ResponseResult addMenu(@RequestBody AddMenuDto addMenuDto){ // @RequestBody 表示数据从请求体来 application/json; 而不是 GET 的 & 拼接格式
        ResponseResult result = sysMenuService.addMenu(addMenuDto);
        return result;
    }

    @DeleteMapping("/{menuId}")
    @ApiOperation("根据 菜单ID，删除菜单")
    @ApiImplicitParam(name = "menuId",value = "菜单ID")
    public ResponseResult delMenuById(@PathVariable("menuId") Integer menuId){

        ResponseResult responseResult = sysMenuService.delMenuById(menuId);

        return responseResult;
    }

    @GetMapping("/{menuId}")
    @ApiOperation("根据 菜单ID，获取菜单")
    @ApiImplicitParam(name = "menuId",value = "菜单ID")
    public ResponseResult selectMenuById(@PathVariable("menuId") Integer menuId){

        ResponseResult responseResult = sysMenuService.selectMenuById(menuId);

        return responseResult;
    }

    @PutMapping/*("/{menuId}")*/// 当前控制层 /system/menu 所以这里不需要加请求路径
    @ApiOperation("根据 菜单内容，修改菜单")
    @ApiImplicitParam(name = "menuListDto",value = "菜单-DTO")
    public ResponseResult selectMenuById(@RequestBody MenuListDto menuListDto){

        ResponseResult responseResult = sysMenuService.saveUpdate(menuListDto);

        return responseResult;
    }


    @GetMapping("/treeselect")
    @ApiOperation("获取所有菜单列表，以树的形式")
    public ResponseResult<List<MenuTreeSelectVo>> getAllMenuByTree(){
        ResponseResult<List<MenuTreeSelectVo>> allMenuByTree = sysMenuService.getAllMenuByTree();

        return allMenuByTree;
    }


    @GetMapping("/roleMenuTreeselect/{id}")
    @ApiOperation("根据角色id，获取菜单列表,和菜单id列表")
    @ApiImplicitParam(name = "id",value = "角色id")
    public ResponseResult<RoleMenuTreeByIdVo> getRoleMenuTreeByID(@PathVariable("id") Long id){ // restful /{id}请求;需要指定参数

        ResponseResult<RoleMenuTreeByIdVo> responseResult =   sysMenuService.getRoleMenuTreeByID(id);

        return responseResult;
    }


}
